import requestApi from "./requestApi";
import { getReqParams } from "./utils";
import { toast } from "nc-lightapp-front";

(function (){
  requestApi.tpl({
      async:false,
      data:"",
      success: (datavalue) => {
          window.rev_defaultValue = datavalue.data[0];
      }
  });
}());

export default function (props) {
  props.createUIDom(
    {
      appcode: "201102JTYF",
      pagecode: "202102LOAN_DETAIL_LIST",
    },
    (data) => {
      let metas = data.template;
      metas['202102JOIN'].items.forEach(item => {
          if (item.attrcode === 'pk_tradetypeid') {
              item.queryCondition = function () {
                  return {
                      GridRefActionExt: 'nccloud.web.erm.action.ref.sqlbuilder.ErmTranstypeRefJKSqlBuilder'
                  };
            };
          }
      })
      props.meta.setMeta(metas);

      props.table.setTableRender('head', 'djbh', (text, record, index) => {
        return (
            <a
                className='hyperlinks'
                onClick={() => {
                  requestApi.link({
                        data: {
                            openbillid : record.pk_jkbx.value,
                            tradetype : record.djlxbm.value,
                            props: props
                        }
                    });
                }}
            >
                {' '}{record.djbh.value}{' '}
            </a>
        );
    });

    props.search.setSearchValByField('202102JOIN', 'bzbm', {
      display: window.rev_defaultValue.name,
      value:  window.rev_defaultValue.pk_currtype
    });

      const sendData = getReqParams(props, metas);
      requestApi.query({
        data: sendData,
        success: (response) => {
          if(!response) {
            return
          }

          calTotal.call(this, response)
          props.table.setAllTableData('head', response.head)


        },
      });
    }
  );
}

function calTotal(response) {
  const totalData = {
    ybjyTotal:0,
    amountRepaidTotal:0,
    repaymentingTotal:0,
    amountToBeRepaidTotal:0,
  }

  if(response) {
    Object.keys(totalData).forEach(key => {
      totalData[key] = response.head.rows[0].values[key]&&response.head.rows[0].values[key].value || 0
    })
  }

  this.setState({total: totalData})
}

export {
  calTotal
}
